﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fawcett.Plugins
{
    public enum PluginCode
    {
        ERROR,
        MESSAGE,
        FROM_FSX,
        TO_FSX,
        FROM_ARDUINO,
        TO_ARDUINO
    }

    public static class GlobalEvent
    {
        public delegate void PluginEventHandler(object sender, EPlugin e);

        public static event PluginEventHandler MyEvent;

        public static void FireEvent(Object o , EPlugin args)
        {
            PluginEventHandler evt = MyEvent;
            if (evt != null)
                evt(o, args);
        }
    }

    public class EPlugin : EventArgs
    {
        private  string     _EventInfo;
        private float _Value;
        private PluginCode pluginCode;




        public  EPlugin(PluginCode code, string Text)
        {
            this.pluginCode = code;
            _EventInfo = Text;

            Console.WriteLine("EPLUGIN (1) : {0}", Text);
        }
        public EPlugin(PluginCode pluginCode, string Offset, bool Value)
        {
            this.pluginCode = pluginCode;
            _EventInfo = Offset;
            _Value = Value ? 1 : 0 ;
        }

        public  EPlugin(PluginCode pluginCode, string Offset, int Value)
        {
            // TODO: Complete member initialization
            this.pluginCode = pluginCode;
            _EventInfo = Offset;
            _Value = Value;

            Console.WriteLine("EPLUGIN  (2) : {0}={1}", Offset, Value);
        }

        public EPlugin(PluginCode pluginCode, string Offset, string Value)
        {
            // TODO: Complete member initialization
            this.pluginCode = pluginCode;
            _EventInfo = Offset;
            try
            {
                Console.WriteLine("EPLUGIN  (3) : {0}={1}", Offset, Value);
                _Value = float.Parse(Value);
            }
            catch (Exception e)
            {
                Console.WriteLine("EPLUGIN  Parse failure {0}" , Value);
                GlobalEvent.FireEvent(this, new EPlugin(PluginCode.ERROR, e.Message));
            }

        }

        public EPlugin(PluginCode pluginCode, string Offset, decimal Value)
        {
            // TODO: Complete member initialization
            this.pluginCode = pluginCode;
            _EventInfo = Offset;
            try
            {
                Console.WriteLine("EPLUGIN  (3) : {0}={1}", Offset, Value);
                _Value = Convert.ToSingle(Value);
            }
            catch (Exception e)
            {
                Console.WriteLine("EPLUGIN  Parse failure {0}", Value);
                GlobalEvent.FireEvent(this, new EPlugin(PluginCode.ERROR, e.Message));
            } this.pluginCode = pluginCode;
     
        }

        public  string GetInfo     {  get { return _EventInfo; }     }
        public PluginCode GetCode { get { return pluginCode; } }

        public string GetOffset { get { return _EventInfo; } }
        public float GetValue { get { return _Value; } }
    }

}
